//
//  HAChooseTabPopView.m
//  HA
//
//  Created by sy on 2023/12/17.
//

#import "HAChooseTabPopView.h"

@interface HAChooseTabPopView ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate,DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;

/***/
@property (nonatomic, strong) NSMutableArray *originalList;
/***/
@property (nonatomic, strong) NSString *title;
/***/
@property (nonatomic, assign) BOOL isSerach;
/***/
@property (nonatomic, strong) UITextField *seachField;

@end

@implementation HAChooseTabPopView


- (instancetype)initWithDicts:(NSArray *)array title:(NSString *)title
{
    self = [super init];
    if (self) {
        [self.originalList addObjectsFromArray:array];
        [self.dataArray addObjectsFromArray:array];
        self.title = title;
        if ([title isEqualToString:@"选择开户银行"]) {
            self.isSerach = YES;
        }
        [self layoutViews];
    }
    return self;
}

#pragma mark - 点击事件
- (void)didTapBackgroundView:(UITapGestureRecognizer *)sender {
    [super didTapBackgroundView:sender];
}

-(void)cancelClick {
    [self dismiss];
}

-(void)show {
    [super show];
}
-(void)dismiss {
    [super dismiss];
}


-(void)layoutViews {
    
    self.frame = CGRectMake(0, 0, kUIScreen_w, kUIScreen_h);
    [self addSubview:self.backgroundView];
    
    //顶部视图
    CGFloat alertViewH = self.dataArray.count * 50 + 50 + 20 + app_bottomH;
    if (alertViewH > kUIScreen_h*0.65) {
        alertViewH = kUIScreen_h*0.65;
    }
    CGFloat alertViewW = kUIScreen_w;
    CGFloat alertViewX = 0;
    CGFloat alertViewY = kUIScreen_h-alertViewH;
    self.alertViewY = alertViewY;
    self.alertView.frame = CGRectMake(alertViewX, alertViewY+20, alertViewW, alertViewH);
    [self addSubview: self.alertView];
    self.alertView.backgroundColor = [UIColor whiteColor];
    self.alertView.layer.cornerRadius = 15;
    self.alertView.layer.masksToBounds = YES;
    
    UILabel *titLabel = [UILabel ha_NLabelWithFont:18 textColor:@"#111A34" text:self.title alignment:1];
    [self.alertView addSubview:titLabel];
    [titLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.centerX.mas_equalTo(0);
        make.height.mas_equalTo(50);
    }];
    
    if (self.isSerach) {
        UIView *seachBgView = [UIView ha_createView];
        seachBgView.layer.cornerRadius = 22;
        seachBgView.layer.masksToBounds = YES;
        seachBgView.backgroundColor = [UIColor khaBGColor];
        [self.alertView addSubview:seachBgView];
        [seachBgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(titLabel.mas_bottom);
            make.left.mas_equalTo(16);
            make.right.mas_equalTo(-16);
            make.height.mas_offset(44);
        }];
        
        UIButton *seachBtn = [[UIButton alloc]init];
        [seachBtn setImage:[UIImage xl_cuttingImageWith:[UIImage imageNamed:@"ha_searchIcon"] toSize:CGSizeMake(22, 22)] forState:UIControlStateNormal];
        [seachBtn addTarget:self action:@selector(seachBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [seachBgView addSubview:seachBtn];
        [seachBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(-12);
            make.centerY.mas_equalTo(0);
            make.width.mas_offset(32);
            make.height.mas_offset(32);
        }];
        
        UITextField *seachField = [[UITextField alloc]init];
        self.seachField = seachField;
        seachField.delegate = self;
        seachField.placeholder = @"请输入开户银行";
        seachField.font = [UIFont systemFontOfSize:18];
        seachField.returnKeyType = UIReturnKeySearch;
        [seachField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
        [seachBgView addSubview:seachField];
        [seachField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(15);
            make.right.equalTo(seachBtn.mas_left).offset(-15);
            make.centerY.mas_equalTo(0);
            make.height.equalTo(@32);
        }];
        
    }
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        if (self.isSerach) {
            make.top.mas_equalTo(titLabel.mas_bottom).offset(44);
        }else {
            make.top.mas_equalTo(titLabel.mas_bottom);
        }
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.bottom.mas_equalTo(-app_bottomH);
    }];
    
    UIButton *cancelButton = [[UIButton alloc]init];
    [self.alertView addSubview:cancelButton];
    [cancelButton setImage:[UIImage imageNamed:@"hl_close01"] forState:UIControlStateNormal];
    [cancelButton addTarget:self action:@selector(cancelClick) forControlEvents:UIControlEventTouchUpInside];
    [self.alertView addSubview:cancelButton];
    [cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.width.mas_offset(50);
        make.height.mas_offset(50);
    }];
    
}

#pragma mark - Table view data source
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *myCcell = [tableView dequeueReusableCellWithIdentifier:@"HAChooseTabPopViewCellID"];
    if (!myCcell) {
        myCcell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"HAChooseTabPopViewCellID"];
    }
    myCcell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSDictionary *dict = self.dataArray[indexPath.row];
    BOOL isline = YES;
    if (indexPath.row == self.dataArray.count-1) {
        isline = NO;
    }
    [self pp_setCellWith:myCcell.contentView dict:dict isLine:isline];
    return myCcell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self dismiss];
    NSDictionary *dict = self.dataArray[indexPath.row];
    NSString *title = dict[@"title"];
    if (self.YHChooseTabMenuCellBlock) {
        self.YHChooseTabMenuCellBlock(title);
    }
    if (self.YHChooseTabMenuCellSureBlock) {
        self.YHChooseTabMenuCellSureBlock(dict);
    }
}

-(void)seachBtnClick {
    [self search];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    [self search];
    return YES;
}

-(void)search {
    [self.seachField resignFirstResponder];
    if ([NSString isEmptyString:self.seachField.text]) {
        [MBProgressHUD showError:@"请输入开户银行"];
    }
    NSString *key = [self.seachField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    self.seachField.text = key;
    [self.dataArray removeAllObjects];
    if (![NSString isEmptyString:key]) {
        for (NSDictionary *dict in self.originalList) {
            NSString *title = dict[@"title"];
            if ([title containsString:key]) {
                [self.dataArray addObject:dict];
            }
        }
    }else {
        [self.dataArray removeAllObjects];
        [self.dataArray addObjectsFromArray:self.originalList];
    }
    [self.tableView reloadData];
}

-(void)textFieldDidChange:(UITextField *)textField{
    if ([NSString isEmptyString:self.seachField.text]) {
        [self.dataArray removeAllObjects];
        [self.dataArray addObjectsFromArray:self.originalList];
        [self.tableView reloadData];
    }
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.seachField resignFirstResponder];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [self.seachField resignFirstResponder];
}


-(void)pp_setCellWith:(UIView *)contentView dict:(NSDictionary *)dict isLine:(BOOL)isLine{
    for (UIView *view in contentView.subviews) {
        [view removeFromSuperview];
    }
    
    NSString *title = @"--";
    if ([dict containsObjectForKey:@"title"]) {
        title = dict[@"title"];
    }
    NSString *term_sn = @"";
    if ([dict containsObjectForKey:@"term_sn"]) {
        term_sn = dict[@"term_sn"];
    }
    NSString *color = @"";
    if ([dict containsObjectForKey:@"color"]) {
        color = dict[@"color"];
    }
    
    NSString *showTitle = @"";
    if (![NSString isEmptyString:term_sn]) {
        showTitle = [NSString stringWithFormat:@"%@(%@)",term_sn,title];
    }else {
        showTitle = title;
    }
    
    UILabel *titLabel = [UILabel ha_NLabelWithFont:17 textColor:color text:showTitle alignment:1];
    [contentView addSubview:titLabel];
    [titLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.centerY.mas_equalTo(0);
    }];
    
    if (isLine) {
        UIView *lineView = [UIView ha_createView];
        lineView.backgroundColor = [UIColor khaBGColor];
        [contentView addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.mas_equalTo(0);
            make.height.mas_offset(1);
        }];
    }
}
- (UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero];
        // 去掉滚动条
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.emptyDataSetSource = self;
        _tableView.emptyDataSetDelegate = self;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.alertView addSubview:_tableView];
    }
    return _tableView;
}

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (NSMutableArray *)originalList
{
    if (!_originalList) {
        _originalList = [NSMutableArray array];
    }
    return _originalList;
}

//设置title
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *title = @"没有搜到任何内容~";
    NSDictionary *attributes = @{
        NSFontAttributeName:[UIFont systemFontOfSize:14],
        NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#999999"]
    };
    return [[NSAttributedString alloc] initWithString:title attributes:attributes];
}

//设置image
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    return [UIImage imageNamed:@"hl_nodata"];
}

@end
